﻿using System;
using System.Windows;
using jSLCore.Client.Presentation.Authorization;
using jSLCore.Client.Presentation.BaseControl;

namespace jSLApp.Client.Forms
{
    public partial class SubscriptionToIt : DataControl
    {
        public SubscriptionToIt()
        {
            InitializeComponent();
        }

        private int? _commentableId;
        public int? CommentableId
        {
            get { return _commentableId; }
            set
            {
                _commentableId = value;
                Refresh();
            }
        }


        private void Refresh()
        {
            if(jSecurity.IsAuthorized)
                if(CommentableId!=null)
                {
                    DoWork(true,(sender, args) =>
                                    {
                                        args.Result = Communication.Proxy.IsSubscripted(args.Int);
                                    },(o, eventArgs) =>
                                                          {
                                                              if(eventArgs.Error==null)
                                                              {
                                                                  Visibility = (bool)eventArgs.Result?Visibility.Collapsed : Visibility.Visible;
                                                              }
                                                              else
                                                              {
                                                                  Visibility = Visibility.Collapsed;
                                                              }
                                                          },CommentableId);
                }
        }

        private void subsript_Clicked(object sender, RoutedEventArgs e)
        {
            if(_commentableId!=null)
            {
                DoWork("Подписка",(o, args) =>
                                      {
                                          Communication.Proxy.CreateSubscription(args.Int);
                                      },(sender1, eventArgs) =>
                                                       {
                                                           if(eventArgs.Error==null)
                                                           {
                                                               Refresh();

                                                               var dlg = new SubscriptionList();
                                                               dlg.ShowDialog(this);
                                                           }
                                                       },_commentableId);
            }
        }
    }
}
